///////////////////////////////////////////////////////////
//  CCreateItemView.cpp
//  Implementation of the Class CCreateItemView
//  Created on:      21-Sep-2013 9:37:27 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CCreateItemView.h"
#include "ControlCommand.h"
#include "stdafx.h"
#include "DataFactory.h"

using namespace UI;
using namespace UI::CMD;

CCreateItemView::CCreateItemView(IWindowDirector* director)
{
  this->m_type = IViewCtrl::CREATEITEMVIEW_T;
  this->m_parrentWnd = director;

  // call initialize for create all UI.
  Initialize();
}


CCreateItemView::~CCreateItemView()
{

}

UI::IViewCtrl::ViewType CCreateItemView::GetType() const 
{
	return this->m_type;
}


void CCreateItemView::Initialize()
{
  this->SetAttribute("x1", 0);
  this->SetAttribute("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "90%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));
  this->SetEventHandler("clickview", this, &CCreateItemView::OnClickedView);

  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Add item")
    .Set("leftButtonCaption", "Back")
    .Set("rightButtonCaption", "Done")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CCreateItemView::OnClickedNevigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  // Create task label
  CLabelPtr textLable = CreateLabel(CAttributes()
    .Set("name",       "textLable")
    .Set("x1",         "4%")
    .Set("y1",         "15%")
    .Set("width",      "92%")
    .Set("height",     "7%")
    .Set("caption",    "Task :")
    .Set("fontSize",   "medium")
    .Set("fontAlignH", "left")
    .Set("fontAlignV", "middle")
    .Set("fontColour", CColour(COLOUR_LABEL)));

  // Add text label in view
  this->AddChild(textLable);

  // Create text field
  this->m_textField = CreateTextField(CAttributes()
    .Set("name",    "TextField1")
    .Set("x1",      "5%")
    .Set("y1",      "23%")
    .Set("width",   "85%")
    .Set("height",  "15%")
    .Set("text",    "")
    .Set("fontSize",    "medium")
    .Set("fontColour",  CColour(COLOUR_BLACK))
    .Set("backgroundColour", CColour(COLOUR_WHITE))
    .Set("fontAlignH",  "left"));

  // Add text field in view
  this->AddChild(this->m_textField);

  // Create type label
  textLable = CreateLabel(CAttributes()
    .Set("name",       "typeLable")
    .Set("x1",         "4%")
    .Set("y1",         "40%")
    .Set("width",      "92%")
    .Set("height",     "7%")
    .Set("caption",    "Type :")
    .Set("fontSize",   "medium")
    .Set("fontAlignH", "left")
    .Set("fontAlignV", "middle")
    .Set("fontColour", CColour(COLOUR_LABEL)));

  // Add text label in view
  this->AddChild(textLable);

  // Create List box item
  CStringArray listBoxItems;
  listBoxItems.AddString(CString("Header"));
  listBoxItems.AddString(CString("Item"));

  // Create List box
  this->m_listBox = CreateListBox(CAttributes()
    .Set("name",    "Listbox sample")
    .Set("x1",      0)
    .Set("y1",      "50%")
    .Set("width",   "100%")
    .Set("height",  "30%")
    .Set("listBoxItems", listBoxItems));

  // Add list box in view
  this->AddChild(this->m_listBox);

  
  

  // Create OK button
  /*CButtonPtr buttonOK = CreateButton(CAttributes()
    .Set("name",    "savebutton")
    .Set("caption", "OK")
    .Set("x1",      "1%")
    .Set("y1",      "82%")
    .Set("width",   "25%")
    .Set("height",  "14%")*/
    //.Set("fontSize", "medium")*/);
  //buttonOK->SetEventHandler("click", this, &CCreateItemView::onAddRowClick);

  // Add ok button in view
  //this->AddChild(buttonOK);
}


void CCreateItemView::Refresh()
{

}


void CCreateItemView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


void CCreateItemView::OnClickedBack()
{

}


void CCreateItemView::OnClickedOK()
{

}


IObject* CCreateItemView::CreateData()
{
  IObject* result = NULL;
  int iSelect = 1; // Item type
  //bool visible = false;
  //this->m_listBox->GetAttribute("visible", visible);
  //if(visible)
  this->m_listBox->GetAttribute("selected", iSelect);
  switch(iSelect)
  {
  case 0: // Header
    {
      result = Data::DataFactory::CreateHeader(GetNameLabel());
    }
    break;
  case 1:
    {
      result = Data::DataFactory::CreateItem(GetNameLabel());
    }
    break;
  default: break;
  }
	return  result;
}


std::string CCreateItemView::GetNameLabel()
{
  CString textValue;
  this->m_textField->GetAttribute("text", textValue);
  return textValue.Get();
}


void CCreateItemView::SetNameLabel(const std::string& text)
{
  this->m_textField->SetAttribute("text", text.c_str());
}


void CCreateItemView::SetItemType(IObject::ObjectType _type)
{

}


void CCreateItemView::ItemTypeEnable(bool enable)
{
  this->m_listBox->SetAttribute("visible",  enable);
  /*if(enable)
  {
    this->m_listBox->SetAttribute("visible",  enable);
  }
  else
  {

  }*/
}

bool UI::CCreateItemView::OnClickedNevigationBar( CNavigationBar* navigationBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT:
    {
      SetNameLabel("");
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
    }
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT:
    {
      ControlCommand::GetInstance()->OnSave(ControlCommand::ITEM_CTRL);
      SetNameLabel("");
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
    }
    break;
  default: break;
  }
  return true;
}

void UI::CCreateItemView::SetTitleView( const std::string& text )
{
  this->m_navigationBar->SetAttribute("title", text.c_str());
}

bool UI::CCreateItemView::OnClickedView( CView* view )
{
  return this->m_parrentWnd->HideKeyboard();
}

void UI::CCreateItemView::SetCurListBox( int index )
{
  if(index > -1 && index < 2)
    this->m_listBox->SetAttribute("selected", index);
  else
    this->m_listBox->SetAttribute("selected", 0);
}
